<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Korisnici
 *
 * @author mateo
 */
class UserRepository {
    public static function addUser($db, $user) {
        if (!UserRepository::checkUserExistance($db, $user->getUsername())) {
            $query = $db->prepare("INSERT INTO User (Username,
            Password, Email, Name, Surname)
            VALUES(?, ?, ?, ?, ?)");
            $query->execute(array(
                $user->getUsername(),
                $user->getPassword(),
                $user->getEmail(),
                $user->getName(),
                $user->getSurname()));
                
        }
    }

    public static function checkUserExistance($db, $username) {
        $query = $db->prepare("SELECT * FROM User WHERE Username = ?");
        $query->execute(array($username));
        if (count($query->fetchAll()) == 0) {
            return false;
        } else {
            return true;
        }
    }
    public static function checkEmailExistance($db, $email) {
        $query = $db->prepare("SELECT * FROM User WHERE Email = ?");
        $query->execute(array($email));
        if (count($query->fetchAll()) == 0) {
            return false;
        } else {
            return true;
        }
    }
    public static function getUser($db, $username) {
        $query = $db->prepare("SELECT * FROM User WHERE Username = ?");
        $query->execute(array($username));

        return UserRepository::htmlSpecialCharactersArray($query->fetchAll());
    }

    public static function removeUserByUsername($db, $username) {
        $query = $db->prepare("DELETE FROM User WHERE Username = ?");
        $query->execute(array($username));
    }
    public static function removeUserByID($db, $id) {
        $query = $db->prepare("DELETE FROM User WHERE ID = ?");
        $query->execute(array($id));
    }

    public static function getAllUsers($db) {
        $dohvati = $db->prepare("SELECT * FROM User");
        $dohvati->execute();
        return UserRepository::htmlSpecialCharactersArray($dohvati->fetchAll());
    }

    public static function checkUser($db, $username, $password) {
        $query = $db->prepare("SELECT * FROM User WHERE Username = ?");
        $query->execute(array($username));
        $users = $query->fetchAll();
        if (isset($users[0])) {
            if ($users[0]['Password'] === $password) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static function editUser($db, $korisnik) {
        $uredi = $db->prepare("UPDATE User SET Username=?, Password=?, Email=?, Name=?, Surname=?  where ID = ?");
        $uredi->execute(array($korisnik->getUsername(), $korisnik->getPassword(),
            $korisnik->getEmail(),$korisnik->getName(),$korisnik->getSurname() ,$korisnik->getID()));
    }

    public static function getUsersUsername($db, $idKorisnika) {
        $dohvati = $db->prepare("SELECT * FROM User WHERE ID = ?");
        $dohvati->execute(array($idKorisnika));
        $users = $dohvati->fetchAll();
        return ($users[0]['Username']);
    }

    public static function getUsersID($db, $username) {
        $query = $db->prepare("SELECT * FROM User WHERE Username = ?");
        $query->execute(array($username));
        $korisnik = $query->fetchAll();
        return $korisnik[0]['ID'];
    }

    private static function htmlSpecialCharactersArray($array) {
        foreach ($array as $outterKey => $entity) {
            foreach ($entity as $innerKey => $value) {
            $result[$outterKey][$innerKey] = htmlspecialchars($value);
            }
        }

        return $result;
    }

}
?>
